﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class OrderBasket : AuthenticationRequiredCustomerBaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected override void PreLoadHandler(object sender, EventArgs e)
		{
			if (this.CheckPrivileges() == false)
				this.Visible = false;
		}

		internal GridView GridView
		{
			get { return this.GridView1; }
		}

		internal void SetEmpty(bool isEmpty)
		{
			this.pnlEmptyMsg.Visible = isEmpty;
			this.btnPay.Visible = !isEmpty;
			this.btnTrash.Visible = !isEmpty;
		}

		protected void GridView1_DataBound(object sender, EventArgs e)
		{
			this.SetEmpty(this.GridView.Rows.Count == 0);
		}

		protected void GridView1_DataBinding(object sender, EventArgs e)
		{
			decimal totCredit;
			decimal totPrice;

			this.OrderBasketData.Sum(out totCredit, out totPrice);

			this.GridView1.Columns[2].FooterText = totCredit.ToString("c2");
			this.GridView1.Columns[3].FooterText = totPrice.ToString("c2");

		}

		protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
		{
			this.OrderBasketData.Clear();

			Response.Redirect(Request.RawUrl, true);
		}

		protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			int offerId = Convert.ToInt32(e.CommandArgument);

			switch (e.CommandName)
			{
				case "DeleteRow":
					this.OrderBasketData.DeleteOffer(offerId);
					break;
			}

			Response.Redirect(Request.RawUrl, true);
		}

	}
}